package ds.factcom.view.console.invoice;

import ds.factcom.controller.invoice.InvoiceController;
import ds.factcom.exception.EmptyListProductException;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.IsNotNumberException;
import ds.factcom.exception.IsNotStringException;
import ds.factcom.exception.NotCorrectDocumentNumberException;
import ds.factcom.exception.NotCorrectDocumentTypeException;
import ds.factcom.exception.NotDefinedClientException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.console.BaseConsoleView;
import ds.factcom.view.validate.ValidatorClient;

/**
 * 
 * @author Javier De Luca, Maximiliano Herrera, Matias Samblancat
 *
 */
public class InvoiceConsoleView extends BaseConsoleView{

	private final InvoiceController invoiceController;
	private String stringAux;
	private String descriptionProduct;
	private boolean desicion = true;
	private boolean moreProduct = true;
	private Integer selection;

	public InvoiceConsoleView(InvoiceController invoiceController) {
		this.invoiceController = invoiceController;
		this.invoiceController.addObserver(this);
	}

	public void createInvoice() {
		System.out.println("-------------------------------------------------------");
		System.out.println("FACTURAR");
		System.out.println("-------------------------------------------------------");
		
		while(desicion){
			try {
				System.out.print("Indique el tipo de documento "+getMessageDocumentType()+" del cliente:");
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentType(stringAux);
				String documentType = stringAux;
				
				System.out.print("Ingrese un Numero de documento: ");
				stringAux = getScanner().nextLine();
				ValidatorClient.isCorrectDocumentNumber(stringAux);
				String documentNumber = stringAux;
				
				this.invoiceController.findByDocument(documentType, documentNumber);
				
				
				while(moreProduct){
					System.out.print("Adhiera el producto:");
					this.stringAux = getScanner().nextLine();
					try{
						ValidatorClient.isNotEmpty(this.stringAux);
						this.descriptionProduct = this.stringAux;
						System.out.println("Ingrese cantidad:");
						this.stringAux = getScanner().nextLine();
						this.invoiceController.addProduct(descriptionProduct, Integer.valueOf(this.stringAux));
					}catch (EmptyStringException e) {
						showErrorMessage(e.getMessage());
					}finally{
						System.out.print("Desea agregar otro producto S/N");
						this.stringAux = getScanner().nextLine();
						if("N".equalsIgnoreCase(this.stringAux)){
							moreProduct = false;
						}
					}
				}
				generateTypeInvioce();
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotStringException e) {
				showErrorMessage(e.getMessage());	
			} catch (NotCorrectDocumentTypeException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotCorrectDocumentNumberException e) {
				showErrorMessage(e.getMessage());
			} catch (NotExistDomainException e) {
				showErrorMessage(e.getMessage());
			}finally{
				System.out.print("Desea facturar otro cliente S/N");
				this.stringAux = getScanner().nextLine();
				if("N".equalsIgnoreCase(this.stringAux)){
					desicion = false;
				}
			}
		}
	}

	private void generateTypeInvioce() {
		System.out.println("-------------------------------------------------------");
		System.out.println("SELECCIONE EL TIPO DE FACTURACION A REALIZAR");
		System.out.println("-------------------------------------------------------");
		System.out.println("1.- Factura Cliente");
		System.out.println("2.- Factura Empresa");
		System.out.println("3.- Factura Vip");
		try {
			stringAux = getScanner().nextLine();
			ValidatorClient.correctSelection(stringAux);
			selection = Integer.valueOf(stringAux);

			switch (selection) {
			case 1:
				this.invoiceController.generateInviocePerson();
				break;
			case 2:
				this.invoiceController.generateInvioceCompany();
				break;
			case 3:
				this.invoiceController.generateInvioceVip();
				break;
			default:
				System.out.println(stringAux+" es un valor equivocado");
			}
		} catch (EmptyStringException e) {
			showErrorMessage(e.getMessage());
		} catch (IsNotNumberException e) {
			showErrorMessage(e.getMessage());
		} catch (EmptyListProductException e) {
			showErrorMessage(e.getMessage());
		} catch (NotDefinedClientException e) {
			showErrorMessage(e.getMessage());
		}
	}
}
